<?php
/*
*	This script was writed by Setec Astronomy - setec@freemail.it
*
*	This script is distributed  under the GPL License
*
*	This program is distributed in the hope that it will be useful,
*	but WITHOUT ANY WARRANTY; without even the implied warranty of
*	MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
* 	GNU General Public License for more details.
*
*	http://www.gnu.org/licenses/gpl.txt
*
*/
define('DEBUG_OK', false);
class Service_CCheckMail {
	var $timeout = 2;
	var $domain_rules = array (
		"aol.com", "bigfoot.com", "brain.net.pk", "breathemail.net", "compuserve.com", "dialnet.co.uk", "glocksoft.com", "home.com", "msn.com", "rocketmail.com", "uu.net", "yahoo.com", "yahoo.de"
	);
	
	protected function _isValidEmail($eMail = "") {
		return preg_match('/^[.\w-]+@([\w-]+\.)+[a-zA-Z]{2,6}$/', $eMail);
	}
	
	protected function _checkDomainRules($domain = "") {
		return in_array(strtolower($domain), $this->domain_rules);
	}
	
	public function execute($eMail = "") {
		if (!$this->_isValidEmail($eMail)) return false;
		
		$host = substr(strstr($eMail, '@'), 1);
		
		//if ($this->_checkDomainRules($host)) return false;
		
		$host .= ".";
		
		if (getmxrr($host, $mxhosts[0], $mxhosts[1]) == true) {
			array_multisort($mxhosts[1], $mxhosts[0]);
		}
		else {
			$mxhosts [0] = $host;
			$mxhosts [1] = 10;
		}
		if (DEBUG_OK) {
			print_r($mxhosts);
		}
		
		$port = 25;
		$localhost = $_SERVER ['HTTP_HOST'];
		$sender = 'temp@hotels.ru';
		
		$result = false;
		$id = 0;
		$time = 0;
		Pressure::setStart();
		while ( !$result && $id < count($mxhosts[0]) && $time < 3) {
			if (function_exists("fsockopen")) {
				if (DEBUG_OK) {
					print_r($id . " " . $mxhosts[0][$id]);
				}
				$connection = @fsockopen($mxhosts[0][$id], $port, $errno, $error, $this->timeout);
				//stream_set_blocking($connection, 0);
				if ($connection) {
					$data = fgets($connection, 1024);
					if (DEBUG_OK) {
						echo 'data 1';
						print_r($data);
					}
					fputs($connection, "HELO " . $localhost . "\r\n"); // 250
					$data = fgets($connection, 1024);
					$response = substr($data, 0, 1);
					if (DEBUG_OK) {
						echo 'data 2';
						print_r($data);
					}
					
					if ($response == '2') {// 200, 250, 354 etc.
						fputs($connection, "MAIL FROM:<" . $sender . ">\r\n");
						$data = fgets($connection, 1024);
						$response = substr($data, 0, 1);
						if (DEBUG_OK) {
							echo 'data 3';
							print_r($data);
						}
						
						if ($response == '2') {// 200, 250 etc.
							fputs($connection, "RCPT TO:<" . $eMail . ">\r\n");
							$data = fgets($connection, 1024);
							$response = substr($data, 0, 1);
							if (DEBUG_OK) {
								echo 'data 4';
								print_r($data);
							}
							
							if ($response == '2') {// 200, 250 etc.
								fputs($connection, "data\r\n");
								$data = fgets($connection, 1024);
								$response = substr($data, 0, 1);
								if (DEBUG_OK) {
									echo 'data 5';
									print_r($data);
								}
								
								if ($response == '2' || $response == '3') {// 200, 250 etc.
									$result = true;
								}
							}
						}
					}
					
					fputs($connection, "QUIT\r\n");
					fclose($connection);
					if ($result) {
						return true;
					}
				}
			}
			else break;
			$id++;
			$time = Pressure::calculateCurrentDuration();
		}
		return false;
	}
}
?>